<?php
/**
 * index.php
 * this is a entry point in this application
 */
ob_start();
/**
 * main settings
 */
date_default_timezone_set('Europe/Warsaw');
//error_reporting(0);
//header('Content-type: application/json; charset=utf-8');
 /**
  * load main configuration file
  */
try {
 	if(file_exists("./inc/mainconfiguration.php")){
 		require_once './inc/mainconfiguration.php';
 	}
 	else {
 		throw new Exception(CAN_NOT_LOAD_FILE."<strong>./configs/mainconfiguration.php</strong>.".FILE_DOES_NOT_EXISTS);
 	}
}
catch(Exception $exception){
 	echo $exception->getMessage();	
}
 session_start();
 session_regenerate_id();
 session_cache_expire(1800);
$_SESSION['access_level'] = 9;
$_SESSION['username'] = "adrian";
//var_dump($_SESSION);
 
/**
 * load language definitions
 */
try {
 	/**
 	 * if isset $_SESSION user_language, framework load current user language
 	 */
 	if(isset($_SESSION['user_language']) && (trim($_SESSION['user_language'] != ''))){
 		$user_language = $_SESSION['user_language'];
 	}
 	/**
 	 * if not isset $_SESSION user_language, framework load default language file
 	 */
 	else {
 		$user_language = "us_US";
 	}
 	
 	if(file_exists("./lang/".$user_language.".php")){
 		require_once './lang/'.$user_language.'.php';
 	}
 	else {
 		throw new Exception("The application can not load the language file: <strong>./lang/"
 				.$user_language.
 				".php</strong><br>".
 				"The file containing the definitions of language probably does not exist!");
 	}
}
catch (Exception $exception){
 	echo $exception->getMessage();
}
 
/**
 * load the router file
 */
/**
 * load main configuration file
 */
try {
	if(file_exists("./router.php")){
		require_once './router.php';
	}
	else {
		throw new Exception(CAN_NOT_LOAD_FILE."<strong>./router.php</strong>.".FILE_DOES_NOT_EXISTS);
	}
}
catch(Exception $exception){
	echo $exception->getMessage();
}
/**
 * Application get all variables into array
 */
$user_data = array (
	'post' => $_POST,
		'get' => $_GET,
		'files' => $_FILES,
		'environment' => $_ENV,
		'request' => $_REQUEST,
		'session' => $_SESSION,
		'cookies' => $_COOKIE,
);
//var_dump($user_data);
/**
 * Application get user request
 */
function __autoload($class_name){
	try {
		/**
		 * Parse out filename where class should be created
		 * This support names like Functionality_Tier_Classname as well as Functionality_Tier_Classname
		 */
		list($file_to_load, $functionality_and_folder) = preg_split("/_/", strrev($class_name), 2);
		$functionality_and_folder = strrev($functionality_and_folder);
		$file_to_load = strrev($file_to_load);
		list($tier, $functionality) = preg_split("/_/", strrev($functionality_and_folder), 2);
		$functionality = strrev($functionality);
		$tier = strrev($tier);
		/**
		 * select the folder where class should be located
		 * bassed on $tier
		 */
		switch (strtolower($tier)){
			case 'model':
				$folder = '/models/';
				break;
			case 'data':
				$folder = '/data/';
				break;
			case 'controller':
				$folder = '/controllers/';
				break;
			case 'library':
				$folder = '/libraries/';
				break;
			case 'driver':
				$folder = "/libraries/drivers/";
				break;
			default:
				//throw new Exception(INVALID_APPLICATION_TIER."<strong>".$tier."</strong>!");
		}
		/**
		 * compose the filename
		 */
		$filename = "./" . strtolower($functionality) . strtolower($folder) . strtolower($file_to_load) . ".php";
		/**
		 * fetch file
		 */
		if(file_exists($filename)){
			/**
			 * get file
			 */
			require_once $filename;
		}
		else {
			/**
			 * file does not exists
			 */
			//throw new Exception(CAN_NOT_LOAD_FILE."<strong>".$filename."</strong>!".FILE_DOES_NOT_EXISTS);
		}
		
	}
	catch (Exception $esception){
		//echo $esception->getMessage();
	}
}
/**
 * load application router
 */
try {
	if(file_exists(WEB_ROOT_FOLDER."router.php")){
		require_once WEB_ROOT_FOLDER.'router.php';
	}
	else {
		throw new Exception(CAN_NOT_LOAD_FILE."<strong>".WEB_ROOT_FOLDER."router.php"."</strong>".FILE_DOES_NOT_EXISTS);
	}
}
catch (Exception $exception){
	echo $exception->getMessage();
}
//var_dump($user_data);
//once we have the controller instantiated, execute the default function
//pass any GET varaibles to the main method
//var_dump($user_data);
$controller->main($user_data);
ob_end_flush();